<template>
    <n-grid cols="1 s:1 m:1 l:4 xl:4 2xl:4" responsive="screen">
      <n-grid-item>
        <n-form :label-width="80" :model="formParams" :rules="rules" ref="formRef">
          
          <!-- <n-form-item label="微信消息推送" path="push_type">
            <n-radio-group v-model:value="formParams.push_type" name="push_type">
              <n-space>
                <n-radio :key="0" :value="0">关闭</n-radio>
                <n-radio :key="1" :value="1">开启</n-radio>
              </n-space>
            </n-radio-group>
          </n-form-item> -->
          
          <n-form-item label="secret_id" path="secret_id">
            <n-input v-model:value="formParams.secret_id" placeholder="请输入secret_id" />
          </n-form-item>
  
          <n-form-item label="secret_key" path="secret_key">
            <n-input v-model:value="formParams.secret_key" placeholder="请输入secret_key" />
          </n-form-item>

          <n-form-item label="region" path="region">
            <n-input v-model:value="formParams.region" placeholder="请输入region" />
          </n-form-item>

          <n-form-item label="桶名称bucket" path="bucket">
            <n-input v-model:value="formParams.bucket" placeholder="请输入bucket" />
          </n-form-item>

          <n-form-item label="app_id" path="app_id">
            <n-input v-model:value="formParams.app_id" placeholder="请输入app_id" />
          </n-form-item>

          <!-- <n-form-item label="备案编号" path="icpCode">
            <n-input placeholder="请输入备案编号" v-model:value="formParams.icpCode" />
          </n-form-item>

          <n-form-item label="手机号授权登录" path="phone_status">
            <n-radio-group v-model:value="formParams.phone_status" name="phone_status">
              <n-space>
                <n-radio :key="0" :value="0">关闭</n-radio>
                <n-radio :key="1" :value="1">开启</n-radio>
              </n-space>
            </n-radio-group>
          </n-form-item> -->

          <div>
            <n-space>
              <n-button type="primary" :loading="subLoading" @click="formSubmit">提交</n-button>
            </n-space>
          </div>
        </n-form>
      </n-grid-item>
    </n-grid>
  </template>
  
  <script lang="ts" setup>
    import { ref, onMounted } from 'vue';
    import { useMessage, FormRules } from 'naive-ui';
    import { cosconfigEdit,cosconfigInfo } from '@/api/webconfig/index';
    
    const rules: FormRules = {
      // push_type: {
      //   required: true,
      //   message: '消息推送',
      //   trigger: 'blur',
      // },
      push_startid: {
        required: true,
        message: '请输入开始充电推送模板id',
        trigger: 'input',
      },
      push_endid: {
        required: true,
        message: '请输入结束充电推送模板id',
        trigger: 'input',
      },
    };
  
    const defaultValueRef = () => ({
        secret_id: '',
        secret_key: '',
        region: '',
        bucket: '',
        app_id: '',
    });
  
    const message = useMessage();
    const formRef: any = ref(null);
    const subLoading = ref(false);
    const formParams = ref(defaultValueRef());
  
    function formSubmit() {
      formRef.value.validate((errors) => {
        if (!errors) {
          cosconfigEdit(formParams.value).then((_) => {
            message.success('更新成功');
          });
        } else {
          message.error('验证失败，请填写完整信息');
        }
      });
    }
  
    function getInfo() {
      cosconfigInfo({}).then((res) => {
        const params = {
            push_type: res.push_type,
            push_startid: res.push_startid,
            push_endid: res.push_endid,
            secret_id: res.secret_id,
            secret_key: res.secret_key,
            region: res.region,
            bucket: res.bucket,
            app_id: res.app_id,
        };
        formParams.value = Object.assign(formParams.value, params);
      });
    }
  
    onMounted(async () => {
      getInfo();
    });
  </script>
  